package com.mercury.service;

import com.alibaba.fastjson.JSONObject;
import com.mercury.dao.IPcattendanceDao;
import com.mercury.dao.IPcorderDao;
import com.mercury.entity.Pcattendance;
import com.mercury.entity.Pcorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PcattendanceService {

    @Autowired
    IPcattendanceDao pcattendanceDao;

    @Autowired
    IPcorderDao pcorderDao;

    //单条记录生成以及更新
    //    public void createPcattendance(JSONObject json) {
    //        String orderId = json.getString("orderId");
    //        Pcattendance attendance = pcattendanceDao.getAttendanceByOrderId(orderId);
    //        if (attendance == null) {
    //            attendance = new Pcattendance();
    //            Pcorder pcorder = pcorderDao.getOrderById(orderId);
    //            attendance.setGuid(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + "0001");
    //            attendance.setName(pcorder.getName());
    //            attendance.setContactTel(pcorder.getContactTel());
    //            attendance.setOrderId(pcorder.getGuid());
    //            attendance.setClassRoomGuid(pcorder.getClassRoomGuid());
    //            attendance.setCheckTimes(1);
    //            pcattendanceDao.create(attendance);
    //        } else {
    //            attendance.setCheckTimes(attendance.getCheckTimes() + 1);
    //            pcattendanceDao.updateTimes(attendance);
    //        }
    //    }

    //生成多条明细
    public void createPcattendance(Pcorder pcorder) {
        Pcattendance attendance = new Pcattendance();
        attendance.setGuid(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + "0001");
        attendance.setName(pcorder.getName());
        attendance.setContactTel(pcorder.getContactTel());
        attendance.setOrderId(pcorder.getGuid());
        attendance.setClassRoomGuid(pcorder.getClassRoomGuid());
        attendance.setOpenId(pcorder.getWxOpenID());
        attendance.setCheckTimes(1);
        attendance.setCreateTime(new Date());
        pcattendanceDao.create(attendance);
    }

    public Pcattendance getAttendance(JSONObject json) {
        return pcattendanceDao.getAttendanceByOrderId(json.getString("orderId"));
    }

    public List<Pcattendance> getAttendanceListByOrderId(JSONObject json){
        return pcattendanceDao.getAttendanceListByOrderId(
                json.getString("orderId")
        );
    }
}
